package com.hp.upload

import com.hp.PathFormat
import com.hp.define.AppInfo
import com.hp.define.BaseState
import com.hp.define.State
import org.apache.commons.fileupload.FileItemStream
import org.apache.commons.fileupload.FileUploadException
import org.apache.commons.fileupload.servlet.ServletFileUpload
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.multipart.MultipartHttpServletRequest

import javax.servlet.http.HttpServletRequest

public class BinaryUploader {

    public static  State save(HttpServletRequest request, Map<String, Object> conf) {
        FileItemStream fileStream = null
        boolean isAjaxUpload = request.getHeader("X_Requested_With") != null
        if (!ServletFileUpload.isMultipartContent(request)) {
            return new BaseState(false, AppInfo.ERROR_5_NOT_MULTIPART_CONTENT)
        } else {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            MultipartFile multipartFile = multipartRequest.getFile(conf.get("fieldName").toString());
            if (multipartFile == null) {
                return new BaseState(false, AppInfo.ERROR_7_NOTFOUND_UPLOAD_DATA)
            }
//
//            ServletFileUpload upload = new ServletFileUpload()
//            if (isAjaxUpload) {
//                upload.setHeaderEncoding("UTF-8")
//            }

            try {
                String savePath = (String) conf.get("savePath")
                String originFileName = multipartFile.getOriginalFilename()
//                String suffix = FileType.getSuffixByFilename(originFileName)//1.jpg
                String suffix = originFileName.substring(originFileName.lastIndexOf(".")).toLowerCase()

                originFileName = originFileName.substring(0,
                        originFileName.length() - suffix.length())
                savePath = savePath + suffix

                long maxSize = ((Long) conf.get("maxSize")).longValue()

                if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
                    return new BaseState(false, AppInfo.ERROR_8_NOT_ALLOW_FILE_TYPE)
                }

                savePath = PathFormat.parse(savePath, originFileName)

                String physicalPath = (String) conf.get("rootPath") + savePath //上传文件地址

                InputStream is = multipartFile.getInputStream()
                State storageState = StorageManager.saveFileByInputStream(is,
                        physicalPath, maxSize)
                is.close()

                String accessPath=(String) conf.get("accessPath") + savePath //访问地址
                if (storageState.isSuccess()) {
                    storageState.putInfo("url", PathFormat.format(accessPath))
                    storageState.putInfo("type", suffix)
                    storageState.putInfo("original", originFileName + suffix)
                }

                return storageState
//                FileItemIterator iterator = upload.getItemIterator(request)
//                println "hasNext:" + iterator.hasNext()
//                while (iterator.hasNext()) {
//                    fileStream = iterator.next()
//                    if (!fileStream.isFormField()) {
//                        break
//                    }
//                    fileStream = null
//                }
//                if (fileStream == null) {
//                    return new BaseState(false, AppInfo.ERROR_7_NOTFOUND_UPLOAD_DATA)
//                } else {
//                    String savePath = (String) conf.get("savePath")
//                    String originFileName = fileStream.getName()
//                    String suffix = FileType.getSuffixByFilename(originFileName)
//                    originFileName = originFileName.substring(0, originFileName.length() - suffix.length())
//                    savePath = savePath + suffix
//                    long maxSize = ((Long) conf.get("maxSize")).longValue()
//                    if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
//                        return new BaseState(false, AppInfo.ERROR_8_NOT_ALLOW_FILE_TYPE)
//                    } else {
//                        savePath = PathFormat.parse(savePath, originFileName)
//                        String physicalPath = (String) conf.get("rootPath") + savePath
//                        InputStream is = fileStream.openStream()
//                        State storageState = StorageManager.saveFileByInputStream(is, physicalPath, maxSize)
//                        is.close()
//                        if (storageState.isSuccess()) {
//                            storageState.putInfo("url", PathFormat.format(savePath))
//                            storageState.putInfo("type", suffix)
//                            storageState.putInfo("original", originFileName + suffix)
//                        }
//
//                        return storageState
//                    }
//                }
            } catch (FileUploadException var14) {
                return new BaseState(false, AppInfo.ERROR_6_PARSE_REQUEST_ERROR)
            } catch (IOException var15) {
                return new BaseState(false, AppInfo.ERROR_4_IO_ERROR)
            }
        }
    }

    private static boolean validType(String type, String[] allowTypes) {
        List<String> list = Arrays.asList(allowTypes)
        return list.contains(type)
    }
}
